package org.cy.shixun.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    //指定redis数据库中的键值对都是字符串
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //在redis数据库中添加一条数据
    public Boolean setString(final String key,String value) {
        Boolean rs = false;
        try {
            redisTemplate.opsForValue().set(key,value);
            redisTemplate.expire(key, 3600, TimeUnit.SECONDS);
            rs = true;
        } catch (Exception e){
            e.printStackTrace();
        }
        return rs;
    }
    //根据key读取redis数据库中的数据
    public String getString(final String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 判断token是否存在
     * @param token 令牌
     * @return boolean
     */
    public boolean exist(String token){
        return redisTemplate.hasKey(token);
    }
}
